package com.vegetable.common.utils;

import com.vegetable.modules.entity.baseData.BankCard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class BankNoRedisService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    @Autowired
    private BankCardRedisService bankCardRedisService;

    // Redis中存储bankNo的set的key
    private static final String BANK_NO_SET_KEY = "address_book:bank_no_set";
    
    // Redis中存储bankNo到addressBook信息的hash的key
    private static final String BANK_NO_ADDRESS_BOOK_HASH_KEY = "address_book:bank_no_map";
    
    // Redis中存储前缀索引的key前缀
    private static final String BANK_NO_PREFIX_KEY = "address_book:bank_no_prefix:";
    
    // Redis中存储后缀索引的key前缀
    private static final String BANK_NO_SUFFIX_KEY = "address_book:bank_no_suffix:";

    /**
     * 初始化bankNo缓存（兼容旧版本，实际使用BankCardRedisService）
     * @param bankCardList bankCard列表
     */
    public void cacheBankNoList(List<BankCard> bankCardList) {
        bankCardRedisService.cacheBankCardList(bankCardList);
    }

    /**
     * 判断bankNo是否存在（支持模糊匹配，兼容旧版本接口）
     * @param bankNo 银行卡号，可能包含*
     * @return 是否存在
     */
    public boolean isBankNoExists(String bankNo) {
        return bankCardRedisService.isBankNoExists(bankNo);
    }

    /**
     * 根据bankNo获取BankCard信息（兼容旧版本接口）
     * @param bankNo 银行卡号
     * @return BankCard信息
     */
    public BankCard getBankCardByBankNo(String bankNo) {
        return bankCardRedisService.getBankCardByBankNo(bankNo);
    }

    /**
     * 根据模糊bankNo获取所有匹配的bankNo列表（兼容旧版本接口）
     * @param fuzzyBankNo 模糊银行卡号，包含*
     * @return 匹配的bankNo列表
     */
    public Set<String> getMatchingBankNos(String fuzzyBankNo) {
        return bankCardRedisService.getMatchingBankNos(fuzzyBankNo);
    }
}